//
//  YHUserAccountViewModel.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/23.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

private let userInfoKey = "com.clover"

class YHUserAccountViewModel: NSObject {
    
    static let sharedAccountViewModel: YHUserAccountViewModel = YHUserAccountViewModel()
    
    var userAccount : YHUserAccount?
    
    private override init() {
        super.init()
        userAccount = loadUserAccount()
    }
    
    var userLogin : Bool {
        
        if userAccount?.expires_date?.compare(NSDate()) == .OrderedDescending {
            
            return true
        }
        
        userAccount = nil
        return false
    }
    
    var iconURL : NSURL? {
        
        get {
            
            return NSURL(string: userAccount?.avatar_large ?? "")
        }
    }

}

extension YHUserAccountViewModel{
        
    func loadAccessToken(code: String, finished: (isSuccess: Bool)->()) {
        
        YHNetworkTools.sharedTools.loadAccessToken(code) { (responseObject, error) in
            
            if error != nil{
                
                print(error)
                
                finished(isSuccess: false)
                
                return
            }
            
            self.loadUserInfo(responseObject, finished: finished)
        }
            
            
    }
   
    private func loadUserInfo(response:AnyObject?, finished: (isSuccess: Bool)->()) {
        
        guard let result = response as? [String: AnyObject] else{
            
            finished(isSuccess: false)
            
            return
        }
        
        YHNetworkTools.sharedTools.loadUserInfo(result) { (responseObject, error) in
            
            if error != nil {
                //                print(error)
                
                finished(isSuccess: false)
                return
            }
            
            //            print(responseObject)
            guard var dict = responseObject as? [String: AnyObject] else{
                
                finished(isSuccess: false)
                return
            }
            
            for (key, value) in result{
                
                dict[key] = value
            }
            
            self.saveUserAccount(dict)
            
            finished(isSuccess: true)
        }
        
    }
    
}

extension YHUserAccountViewModel {
    
    func saveUserAccount(dict: [String: AnyObject]){
        
        let user = YHUserAccount(dict:dict)
        
        self.userAccount = user
        
        let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"]
        
        let userInfo = user.dictionaryWithValuesForKeys(keys)
        
        let userDefaults = NSUserDefaults.standardUserDefaults()
        
        userDefaults.setObject(userInfo, forKey: userInfoKey)
        
        userDefaults.synchronize()
        
//        print(NSHomeDirectory())
        
    }
    
    func loadUserAccount()-> YHUserAccount? {
//        print(NSHomeDirectory())
        guard let dict = NSUserDefaults.standardUserDefaults().objectForKey(userInfoKey) as? [String : AnyObject] else{
            
            return nil
        }
        
        let user = YHUserAccount(dict: dict)
        //        print(user)
        return user
        
        
    }
    

}
        
